#!/bin/sh
#
# Copyright (c) 2014 The Linux Foundation. All rights reserved.
# Copyright (C) 2011-2012 OpenWrt.org
#

[ -e /etc/config/ubootenv ] && exit 0

touch /etc/config/ubootenv

. /lib/ipq806x.sh
. /lib/uboot-envtools.sh
. /lib/functions.sh

board=$(ipq806x_board_name)

default_uboot_env () {
	UBOOTENV_PART=$(cat /proc/mtd | grep APPSBLENV)
	mtd_dev=$(echo $UBOOTENV_PART | awk '{print $1}' | sed 's/:$//')
	mtd_size=0x$(echo $UBOOTENV_PART | awk '{print $2}')
	mtd_erase=0x$(echo $UBOOTENV_PART | awk '{print $3}')
	# size is hardcoded to 0x40000 in u-boot
	uboot_env_size=0x40000
	sectors=$(( $uboot_env_size / $mtd_erase ))

	echo /dev/$mtd_dev 0x0 $uboot_env_size $mtd_erase $sectors
}

case "$board" in
ap148 | \
db149 | \
db147)
	env=$(default_uboot_env)
	[ -n "${env}" ] && ubootenv_add_uci_config $env
	;;
esac

config_load ubootenv
config_foreach ubootenv_add_app_config ubootenv

exit 0
